package com.xdq.config;

import com.xdq.bean.BaseEntity;
import lombok.extern.log4j.Log4j;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.mongodb.core.mapping.event.AbstractMongoEventListener;
import org.springframework.data.mongodb.core.mapping.event.BeforeSaveEvent;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.Objects;

/**
 * @author: 徐东强
 * @date: 2018/9/27 下午2:21
 * @description:
 **/
@Component
@Slf4j
public class BeforeSaveListener extends AbstractMongoEventListener<BaseEntity> {

    @Override
    public void onBeforeSave(BeforeSaveEvent<BaseEntity> event) {
        log.info("update source datetime");
        if (Objects.isNull(event.getSource().getCreateDate())){
            event.getSource().setCreateDate(LocalDateTime.now());
        }
        event.getSource().setUpdateDate(LocalDateTime.now());

        super.onBeforeSave(event);
    }
}
